namespace Sage.Http.Extensions
{
    /// <summary>
    /// 为HttpClient提供下载功能的扩展方法
    /// </summary>
    [Obsolete("此类已过时，请使用HttpExtensions类中的下载方法")]
    public static class HttpDownloadExtensions
    {
        /// <summary>
        /// 下载文件并报告进度
        /// </summary>
        /// <param name="client">HTTP客户端</param>
        /// <param name="requestUri">请求URI</param>
        /// <param name="destinationPath">目标文件路径</param>
        /// <param name="progress">进度报告接口</param>
        /// <param name="bufferSize">缓冲区大小，默认81920字节(80KB)</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>下载的总字节数</returns>
        /// <exception cref="ArgumentNullException">参数为null时</exception>
        /// <exception cref="HttpRequestException">HTTP请求失败时</exception>
        /// <exception cref="IOException">文件I/O错误时</exception>
        [Obsolete("此方法已过时，请使用HttpExtensions.DownloadFileAsync方法")]
        public static Task<long> DownloadFileAsync(
            this HttpClient client,
            string requestUri,
            string destinationPath,
            IProgress<float>? progress = null,
            int bufferSize = 81920,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(client);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);
            ArgumentException.ThrowIfNullOrEmpty(destinationPath);

            // 确保目标目录存在
            var directory = Path.GetDirectoryName(destinationPath);
            if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            // 使用FileMode.Create以确保文件被创建或覆盖
            using var fileStream = new FileStream(
                destinationPath,
                FileMode.Create,
                FileAccess.Write,
                FileShare.None,
                bufferSize,
                useAsync: true);

            return DownloadToStreamAsync(
                client,
                requestUri,
                fileStream,
                progress,
                bufferSize,
                cancellationToken);
        }

        /// <summary>
        /// 下载内容到流并报告进度
        /// </summary>
        /// <param name="client">HTTP客户端</param>
        /// <param name="requestUri">请求URI</param>
        /// <param name="destination">目标流</param>
        /// <param name="progress">进度报告接口</param>
        /// <param name="bufferSize">缓冲区大小，默认81920字节(80KB)</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>下载的总字节数</returns>
        /// <exception cref="ArgumentNullException">参数为null时</exception>
        /// <exception cref="ArgumentException">目标流不可写时</exception>
        /// <exception cref="HttpRequestException">HTTP请求失败时</exception>
        [Obsolete("此方法已过时，请使用HttpExtensions.DownloadToStreamAsync方法")]
        public static Task<long> DownloadToStreamAsync(
            this HttpClient client,
            string requestUri,
            Stream destination,
            IProgress<float>? progress = null,
            int bufferSize = 81920,
            CancellationToken cancellationToken = default)
        {
            return HttpExtensions.DownloadToStreamAsync(client, requestUri, destination, progress, bufferSize, cancellationToken);
        }
    }
}